package com.eryos.android.cigarettecounter.database;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;


public class CigaretteDatabase extends DBConnector {
	
	private static final String CSV_SEPARATOR = ";";
	
	private static CigaretteDatabase currentDB;
	
	private WeeklyAdapterImpl weeklyAdapter;
	private MonthlyAdapterImpl monthlyAdapter;
	private DailyAdapterImpl dailyAdapter;
	
	/**
	 * Create a new CigaretteDatabase Object link to the context in argument
	 * @param ctx Application context
	 */
	protected CigaretteDatabase(Context ctx) {
		super(ctx);
		weeklyAdapter = new WeeklyAdapterImpl(this);
		monthlyAdapter = new MonthlyAdapterImpl(this);
		dailyAdapter = new DailyAdapterImpl(this);
	}
	
	public WeeklyAdapterImpl getWeeklyAdapter(){
		return weeklyAdapter;
	}
	public ExtendedPeriodicAdapterInterface getMonthlyAdapter(){
		return monthlyAdapter;
	}
	public DailyAdapterImpl getDailyAdapter(){
		return dailyAdapter;
	}
	
	public static CigaretteDatabase getInstance(Context ctx) {
		if ( currentDB == null ){
			currentDB = new CigaretteDatabase(ctx);
		}
		
		return currentDB;
	}
	
	public String getDatabaseName(){
		return DBConnector.DATABASE_TABLE;
	}


    /** ************************************************************************** **/    
 
    /**
     * Insert 'now' in database
     * @param reason : reason / label / description of the moment
     * @param price : unit price of cigarette
     */
    public void insertCurrentTimestamp(String _reason, float _price){
    	Log.d(getClass().getSimpleName(), "Insert Current TS");
    	long epoch = System.currentTimeMillis()/1000;
    	
    	insertTimestamp(_price, epoch, _reason);
    }
    
    /**
     * Insert a specific timestamp in DB
     * @param price : unit price of cigarette
     * @param timestamp : time to insert in DB
     * @param reason : reason / label / description of the moment
     */
    public void insertTimestamp(float price, long timestamp, String reason){
		db.execSQL("INSERT INTO "+DBConnector.DATABASE_TABLE+" ( smoke_time, smoke_reason, smoke_valid ) VALUES ( datetime("+timestamp+", 'unixepoch', 'localtime'), '"+reason+"', "+price+");");
		//db.execSQL("INSERT INTO smoke_table ( smoke_time, smoke_reason, smoke_valid ) VALUES ('"+year+"-"+sMonth+"-"+sDay+" "+sHour+":"+sMinute+":"+sSecond+"', 'Random', 'Y');");
    }
    
    /**
     * Delete all data from database
     */
    public void deleteAll(){
    	Log.d(getClass().getSimpleName(), "Delete All");
		db.execSQL("DELETE FROM "+DBConnector.DATABASE_TABLE+";");
    }
	
    /** ************************************************************************** **/    

	/**
	 * Export data to String version (list)
	 * @return
	 */
	public List<String> exportData(){
		Log.d(getClass().getSimpleName(), "Export data");
		
		ArrayList<String> data = new ArrayList<String>();
		
		data.add("Smoke Time"+CSV_SEPARATOR+"Smoke Reason"+CSV_SEPARATOR+"Smoke Price");

		try {
			String sqlQuery = 
				"SELECT strftime('%Y-%m-%d %H:%M:%S', "+DBConnector.TIME_COLUMN+") , "+DBConnector.REASON_COLUMN+", "+DBConnector.PRICE_COLUMN+" "+
				"FROM  "+ getDatabaseName() +"; ";

			Cursor mCursor = db.rawQuery(sqlQuery, null);

			if ( mCursor != null ){
				mCursor.moveToFirst();
				
				while (mCursor != null && ! mCursor.isAfterLast()) {
					String line = mCursor.getString(0)+ CSV_SEPARATOR +mCursor.getString(1)+ CSV_SEPARATOR +mCursor.getFloat(2);
					data.add(line);
					mCursor.moveToNext();
				}				
			}
			Log.d(getClass().getSimpleName(), data.size()+"line(s) exported");
			

		} catch (Exception e) {
			Log.e(this.getClass().getSimpleName(), "Export Data", e);
		}
		
		return data;
	}

	/**
	 * Method used to store in DB all data contained in the ArrayList in param
	 * @param data : List of data in CSV format
	 * @return number of lines inserted
	 */
	public int importData(List<String> data){
		Log.d(this.getClass().getSimpleName(), "Import Data");
		int nbImport = 0;
		for ( String line : data){
			
			if ( ! line.startsWith("Smoke") ){
				String[] tmpValues = line.split(CSV_SEPARATOR);
				
				String sqlQuery = "INSERT INTO "+ getDatabaseName() +" ( "+DBConnector.ROWID_COLUMN+", "+DBConnector.TIME_COLUMN+", "+DBConnector.REASON_COLUMN+", "+DBConnector.PRICE_COLUMN+" ) VALUES (null, '"+tmpValues[0]+"', '"+tmpValues[1]+"', '"+tmpValues[2]+"');";
		    	db.execSQL(sqlQuery);
		    	nbImport++;
			}
		}

		Log.d(getClass().getSimpleName(), nbImport+"line(s) imported");
		return nbImport;
	}

}